package com.juvepoland.jpdroid.activities;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.adapters.LazyCommentAdapter;
import com.juvepoland.jpdroid.application.JPdroidApplication;
import com.juvepoland.jpdroid.common.ImageLoader;
import com.juvepoland.jpdroid.common.Utils;
import com.juvepoland.jpdroid.managers.impl.CommentManager;
import com.juvepoland.jpdroid.managers.impl.NewsManagerImpl;
import com.juvepoland.jpdroid.models.Comment;
import com.juvepoland.jpdroid.models.News;
import com.juvepoland.jpdroid.models.NewsType;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SendCommentActivity extends BasicActivity {

	protected News news;
	protected NewsManagerImpl newsManager = new NewsManagerImpl();
	protected CommentManager commentManager = new CommentManager();
	protected TextView newsTitleTextView;
	protected TextView newsDateTextView;
	protected TextView commentInfoView;
	protected Button sendCommentButton;
	protected EditText editText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment_news_layout);
		Intent intent = getIntent();
		int newsId = intent.getIntExtra("news_id", 0);
		
		if(newsId!=0){
			news = newsManager.getNews(newsId);
			newsTitleTextView = (TextView) findViewById(R.id.title_news_comment);
			newsTitleTextView.setTypeface(basicFontTypeFace);
			newsDateTextView = (TextView) findViewById(R.id.date_single);
			newsDateTextView.setTypeface(basicFontTypeFace);
			
			
			
			newsTitleTextView.setText(news.getTitle());
			commentInfoView = (TextView) findViewById(R.id.comment_info);
			JPdroidApplication app = (JPdroidApplication)getApplicationContext();
			commentInfoView.setText("Komentuje "+app.getLoggedUser().getUsername()+" :");
			newsDateTextView.setText(news.getStringDate());

			ImageLoader il = new ImageLoader(getApplicationContext());
			ImageView thumb_image=(ImageView)findViewById(R.id.single_image); // thumb image
			il.DisplayImage("http://www.juvepoland.com/images/news/"+news.getId()+".jpg", thumb_image);
			sendCommentButton = (Button) findViewById(R.id.sendCommentButton);
			sendCommentButton.setTypeface(basicFontTypeFace);
			editText = (EditText) findViewById(R.id.editText1);
			editText.setTypeface(basicFontTypeFace);
			if(isReply()){
				editText.setText("@"+intent.getStringExtra("reply_username")+" \n ", TextView.BufferType.EDITABLE);
			}
			
			addListenerOnSendCommentButton();
		}		
		
	}

	private boolean isReply() {
		return getIntent().getStringExtra("reply_username") !=null;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.layout.comment_news_menu, menu);
		return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		JPdroidApplication app = (JPdroidApplication)getApplicationContext();
		MenuItem replyMenuItem = menu.findItem(R.id.reply_bookmark);
		if(app.isUserLogged()){
			if(replyMenuItem!=null){
				replyMenuItem.setVisible(true);
			}
		}
		else{
			if(replyMenuItem!=null){
				replyMenuItem.setVisible(false);
			}
		}
		return super.onPrepareOptionsMenu(menu);
	}

	private void addListenerOnSendCommentButton() {
		sendCommentButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new AsyncActionClass().execute(null);
				sendCommentButton.setClickable(false);
			}
		});
		
	}

	@Override
	protected void doInBackgroundAsyncProcess() throws Exception {
		if(commentContainsText()){
			Comment comment = new Comment();
			comment.setContent(Utils.replacePolishCharacters(editText.getText().toString()));
			comment.setDate(new Date());
			JPdroidApplication app = (JPdroidApplication)getApplicationContext();
			comment.setUser(app.getLoggedUser());
			comment.setNews(news);
			comment.setNewsType(NewsType.News);
			
			commentManager.saveComment(comment);
			Intent returnToNewsIntent = new Intent(getApplicationContext(), SingleNewsActivity.class);
			returnToNewsIntent.putExtra("news_id", news.getId());
			startActivity(returnToNewsIntent);
			finish();
		}
		else{
			Toast.makeText(getApplicationContext(), "Nie wprowadzono nic do komentarza!", Toast.LENGTH_LONG);
		}
		
	
	}

	private boolean commentContainsText() {
		return !(editText.getText().toString() == null || editText.getText().toString().trim().length()==0);
	}

	@Override
	protected Intent getIntentToReturnAfterLogout() {
		Intent intent = new Intent(getApplicationContext(), SingleNewsActivity.class);
		intent.putExtra("news_id", news.getId());
		return intent;
	}
	
	
	
	

}
